package lab03.test14;

import java.lang.instrument.ClassDefinition;
import java.sql.*;

/**
 * Created by dell on 2017/11/30.
 */
public class Test14 {

    public static void main(String[] args) throws ClassNotFoundException {
        Connection connection=null;

        Statement statement=null;

        ResultSet resultSet=null;

        String sql="select * from t_userinfo";

            Class.forName("com.mysql.jdbc.Driver");
        try {
            connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/java_2017?serverTimezone=UTC",
                    "root",
                    "123456"
            );
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        try {
            statement = connection.createStatement();
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        try {
            statement.execute("INSERT INTO  t_userinfo VALUES ('7101','宋晓梅','123456','xiaomei@yahoo.com.cn')");
            statement.execute("INSERT INTO  t_userinfo VALUES ('7102','关松杰','654321','songjie@263.net')");
            statement.execute("INSERT INTO  t_userinfo VALUES ('7103','李晓梅','123654','xiaomei@yahoo.com.cn')");
            statement.execute("INSERT INTO  t_userinfo VALUES ('7105','王亚杰','321456','yajie@hotmail.com')");
            statement.execute("INSERT INTO  t_userinfo VALUES ('7106','曲小声','654321','xiaosheng@263.net')");
            resultSet = statement.executeQuery("SELECT * FROM t_userinfo");
        } catch (SQLException e1) {
            e1.printStackTrace();
        }

        try {

            while(resultSet.next()){
                System.out.print(resultSet.getInt(1)+"\t");
                System.out.print(resultSet.getString(2)+"\t");
                System.out.print(resultSet.getString(3)+"\t");
                System.out.print(resultSet.getString(4)+"\t");
                System.out.println();
            }
            statement.execute("INSERT INTO  t_userinfo VALUES ('7107','李晓华','123546','xiaohua@qq.com')");
            System.out.println("after insert:");
            resultSet = statement.executeQuery("SELECT * FROM t_userinfo");
            while(resultSet.next()){
                System.out.print(resultSet.getInt(1)+"\t");
                System.out.print(resultSet.getString(2)+"\t");
                System.out.print(resultSet.getString(3)+"\t");
                System.out.print(resultSet.getString(4)+"\t");
                System.out.println();
            }} catch (SQLException e1) {
            e1.printStackTrace();
        }

    }
}
